<?php
/**
 * Created by PhpStorm.
 * User: Lizhi
 * Date: 2019/3/28
 * Time: 11:23
 */
namespace APP\Libraries;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Http\Request;

class Tool
{
    public function jiami($len = 10, $keyword = '') {
        if (strlen($keyword) > $len) {//关键字不能比总长度长
            return false;
        }
        $str = '';
        $chars = '_abcdefghijkmnpqrstuvwxyz1234567890'; //去掉1跟字母l防混淆
        if ($len > strlen($chars)) {//位数过长重复字符串一定次数
            $chars = str_repeat($chars, ceil($len / strlen($chars)));
        }
        $chars = str_shuffle($chars); //打乱字符串
        $str = substr($chars, 0, $len);
        if (!empty($keyword)) {
            $start = $len - strlen($keyword);
            $str = substr_replace($str, $keyword, mt_rand(0, $start), strlen($keyword)); //从随机位置插入关键字
        }
        return $str;
    }

    public function substrs($num)
    {
        return substr(hash_hmac('md5', $num, 'zh2016715qazyhn6Tsla3kxk83xs2saaa3sx2'), -2);
    }

    //返回值
    public function content($code,$errorMessages,$list)
    {
        return response()->json([
            "code" => $code,
            "errorMessages" =>$errorMessages,
            'content'=>$list
        ],200);
    }

    //直销端预付订单总额返回值
    public function total_prepaid_content($code,$list,$total,$pay_total)
    {
        return response()->json([
            "code" => $code,
            "total" => $total,
            'content'=>$list,
            'pay_total'=>$pay_total
        ],200);
    }

    //商品详情返回值
    public function money_order_content($code,$list,$total,$capital)
    {
        return response()->json([
            "code" => $code,
            "total" => $total,
            'content'=>$list,
            'capital'=>$capital
        ],200);
    }

    //验证扫码返回值
    public function messages_content($code,$list,$total,$system)
    {
        return response()->json([
            "code" => $code,
            "total" => $total,
            'content'=>$list,
            'notification_type'=>$system
        ],200);
    }

    //查询返回值pda
    public function pda_content($code,$list,$total,$errorMessages)
    {
        return response()->json([
            "code" => $code,
            "total" => $total,
            "errorMessages" =>$errorMessages,
            'content'=>$list
        ],200);
    }


    //操作唯一性返回值
    public function only_operation($Messages,$code)
    {
        switch ($code)
        {
            case 20000:
                $status=200;
                $result_only=true;
                break;
            case 20001:
                $status=400;
                $result_only=false;
                break;
            default:
                //其他
        }

        return response()->json([
            "code" => $code,
            "errorMessages" => $Messages,
            "result" => $result_only
        ],$status);
    }
    //操作返回值
    public function operation($code,$Messages)
    {
        switch ($code)
        {
            case 20000:
                $status=200;
                break;
            case 20001:
                $status=400;
                break;
            default:
                //其他
        }
        return response()->json([
            "code" => $code,
            "errorMessages" => $Messages
        ],$status);
    }

    //操作返回值(提示)
    public function operation_code($code,$Messages)
    {
        switch ($code)
        {
            case 20002:
                $status=400;
                break;
            default:
                //其他
        }
        return response()->json([
            "code" => $code,
            "errorArr" => $Messages,
        ],$status);
    }

    //操作返回值(移库)
    public function operation_movegoods($code,$Messages,$data,$ch)
    {
        return response()->json([
            "code" => $code,
            "errorMessages" =>'',
            'content'=>$Messages,
            'data_arr'=>$data,
            'ch'=>$ch
        ],200);
    }

    //分页
    public function page($page,$size,$datalist)
    {
        //每页的条数
        $perPage = $size;
        //计算每页分页的初始位置
        $offset = ($page * $perPage) - $perPage;
        if ($page) {
            $current_page = $page;
            $current_page = $current_page <= 0 ? 1 :$current_page;
        } else {
            $current_page = 1;
        }
        $item = array_slice($datalist, ($current_page-1)*$perPage, $perPage); //注释1
        $total = count($datalist);
        $data =new LengthAwarePaginator($item, $total, $perPage, $current_page);
        $M = $data->toArray()['data'];
        return $M;
    }

    //获取token参数
    public function jwts()
    {
        return JWTAuth::toUser(JWTAuth::getToken());
    }

    //日期操作
    public function datas($state,$time)
    {
        switch ($state)
        {
            case 1:
                return date("Y-m-d H:i:s", time());//输出当前日期
                break;
            case 2:
                return strtotime($time)*1000;//时间变更时间戳*1000
                break;
            case 3:
                return date("Y-m-d H:i:s", $time/1000);//时间戳转时间
                break;
            case 4:
                return date("Ymd", time());//输出当前年月日
                break;
            default:
                //其他
        }
    }

    //查询指定条数数据
    public function index_number($table,$where,$name,$where2,$name2,$orderBy,$number)
    {
        return DB::table($table)->where($where,$name)->where($where2,$name2)->orderBy($orderBy,'DESC')->limit($number)->get()->toArray();
    }

    //查询指定条数数据三
    public function index_number_three($table,$where,$name,$where2,$name2,$where3,$name3,$orderBy,$number)
    {
        return DB::table($table)->where($where,$name)->where($where2,$name2)->where($where3,$name3)->orderBy($orderBy,'DESC')->limit($number)->get()->toArray();
    }

    //查询字段无条件
    public function field($table,$value)
    {
        return DB::table($table)->value($value);
    }

    //查询字段
    public function index_field($table,$field,$name,$value)
    {
        return DB::table($table)->where($field,$name)->value($value);
    }

    //查询一列
    public function index_one_pluck($table,$field,$name,$value)
    {
        return DB::table($table)->where($field,$name)->pluck($value);
    }

    //查询一列两条件
    public function index_two_pluck($table,$field,$name,$field1,$name1,$value)
    {
        return DB::table($table)->where($field,$name)->where($field1,$name1)->pluck($value)->toArray();
    }

    //查询一列三条件
    public function index_three_pluck($table,$field,$name,$field1,$name1,$field2,$name2,$value)
    {
        return DB::table($table)->where($field,$name)->where($field1,$name1)->where($field2,$name2)->pluck($value)->toArray();
    }

    //查询一列
    public function index_pluck($table,$value)
    {
        return DB::table($table)->pluck($value);
    }

    //查询最小值
    public function index_two_min($table,$field,$name,$field1,$name1,$value)
    {
        return DB::table($table)->where($field,$name)->where($field1,$name1)->min($value);
    }

    //一条
    public function first($table)
    {
        return DB::table($table)->first();
    }

    //查询一条
    public function index_first($table,$field,$name)
    {
        return DB::table($table)->where($field,$name)->first();
    }
    //查询一条（一限制条件）
    public function index_one_first($table,$value,$judge,$name)
    {
        $list=DB::table($table)->where($value,$judge,$name)->first();
        return $list;
    }
    //查询一条（二限制条件）
    public function index_many_first($table,$value,$judge,$name,$value2,$judge2,$name2)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->first();
        return $list;
    }

    //查询一条（三限制条件）
    public function index_three_first($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->orderBy($orderBy,'DESC')->first();
        return $list;
    }

    //查询一条（四限制条件）
    public function index_many_four_first($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$value4,$judge4,$name4)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where($value4,$judge4,$name4)->where('soft_deletion',2)->first();
        return $list;
    }

    //查询一条（五限制条件）
    public function index_many_five_first($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$value4,$judge4,$name4,$value5,$judge5,$name5)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where($value4,$judge4,$name4)->where($value5,$judge5,$name5)->where('soft_deletion',2)->first();
        return $list;
    }

    //新建查询是否存在
    public function index_count($table)
    {
        return DB::table($table)->count();
    }

    //新建查询是否存在
    public function index_field_exis($table,$field,$name)
    {
        return DB::table($table)->where($field,$name)->count();
    }

    //新建查询是否存在
    public function index_two_exis_count($table,$field,$name,$field1,$name1)
    {
        return DB::table($table)->where($field,$name)->where($field1,$name1)->count();
    }

    //查询(日期)
    public function index_one_date_count($table,$where,$orderData,$orderData2)
    {
        return DB::table($table)->whereBetween($where, [$orderData,$orderData2])->count();
    }

    //查询数量两条件
    public function index_two_exis($table,$value,$judge,$name,$value1,$name1,$name2)
    {
        return DB::table($table)->where($value,$judge,$name)->whereBetween($value1,[$name1,$name2])->where('soft_deletion',2)->count();
    }

    //查询数量三条件
    public function index_many_exis_contain($table,$value,$name,$value1,$name2,$value2,$name3,$name4)
    {
        return DB::table($table)->where($value,$name)->where($value1,$name2)->whereBetween($value2,[$name3,$name4])->where('soft_deletion',2)->count();
    }

    public function index_many_exis($table,$field,$name,$field1,$name1,$field2,$name2)
    {
        return DB::table($table)->where($field,$name)->where($field1,$name1)->where($field2,$name2)->where('soft_deletion',2)->count();
    }

    //编辑查询是否存在
    public function index_field_edit_exis($table,$ic,$inputic,$field,$name)
    {
        return DB::table($table)->where($ic,'!=',$inputic)->where($field,$name)->count();
    }
    //首页显示（限制条件）
    public function index_list($table,$value,$judge,$name,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->orderBy($orderBy,'DESC')->get()->toArray();
        return $list;
    }

    //首页显示（二限制条件）
    public function index_two_list($table,$value,$judge,$name,$value1,$judge1,$name1,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value1,$judge1,$name1)->orderBy($orderBy,'DESC')->get()->toArray();
        return $list;
    }

    //首页显示（二限制条件）
    public function index_two_list_by($table,$value,$judge,$name,$value1,$judge1,$name1,$orderBy,$by)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value1,$judge1,$name1)->orderBy($orderBy,$by)->get()->toArray();
        return $list;
    }

    //首页显示（二限制条件）
    public function index_pda_two_pluck($table,$value,$judge,$name,$value1,$judge1,$name1,$value3)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value1,$judge1,$name1)->where('soft_deletion',2)->pluck($value3)->toArray();
        return $list;
    }

    //首页显示（三限制条件）
    public function index_many_list($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->orderBy($orderBy,'DESC')->get()->toArray();
        return $list;
    }

    //首页显示（四限制条件）
    public function index_many_four_list($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$value4,$judge4,$name4,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where($value4,$judge4,$name4)->where('soft_deletion',2)->orderBy($orderBy,'ASC')->get()->toArray();
        return $list;
    }

    //首页显示（五限制条件）
    public function index_many_five_list($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$value4,$judge4,$name4,$value5,$judge5,$name5,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where($value4,$judge4,$name4)->where($value5,$judge5,$name5)->where('soft_deletion',2)->orderBy($orderBy,'DESC')->get()->toArray();
        return $list;
    }

    //首页显示（六限制条件）
    public function index_many_six_list($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$value4,$judge4,$name4,$value5,$judge5,$name5,$value6,$judge6,$name6,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where($value4,$judge4,$name4)->where($value5,$judge5,$name5)->where($value6,$judge6,$name6)->where('soft_deletion',2)->orderBy($orderBy,'DESC')->get();
        return $list;
    }

    //首页显示（六限制条件）
    public function index_many_seven_list($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$value4,$judge4,$name4,$value5,$judge5,$name5,$value6,$judge6,$name6,$value7,$judge7,$name7,$orderBy)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where($value4,$judge4,$name4)->where($value5,$judge5,$name5)->where($value6,$judge6,$name6)->where($value7,$judge7,$name7)->where('soft_deletion',2)->orderBy($orderBy,'DESC')->get();
        return $list;
    }

    //pda首页显示（二限制条件）
    public function index_pda_two_list($table,$value,$judge,$name,$value1,$judge1,$name1,$orderBy,$DESC)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value1,$judge1,$name1)->where('soft_deletion',2)->orderBy($orderBy,$DESC)->get();
        return $list;
    }

    //pda首页显示（三限制条件）
    public function index_pda_three_list($table,$value,$judge,$name,$value1,$judge1,$name1,$value2,$judge2,$name2,$orderBy,$DESC)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value1,$judge1,$name1)->where($value2,$judge2,$name2)->where('soft_deletion',2)->orderBy($orderBy,$DESC)->get()->toArray();
        return $list;
    }

    //pda首页显示（限制条件）
    public function index_pda_list($table,$value,$judge,$name,$orderBy,$DESC)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where('soft_deletion',2)->orderBy($orderBy,$DESC)->get();
        return $list;
    }


    //pda查询一条（二限制条件）
    public function index_pda_three_first($table,$value,$judge,$name,$value2,$judge2,$name2,$value3,$judge3,$name3,$orderBy,$DESC)
    {
        $list=DB::table($table)->where($value,$judge,$name)->where($value2,$judge2,$name2)->where($value3,$judge3,$name3)->where('soft_deletion',2)->orderBy($orderBy,$DESC)->first();
        return $list;
    }

    //首页显示
    public function lists($table,$orderBy)
    {
        $list=DB::table($table)->orderBy($orderBy,'DESC')->get()->toArray();
        return $list;
    }

    //首页条件
    public function index_where_list($table,$value1,$name1,$value2,$name2,$orderBy)
    {
        $list=DB::table($table)->where($value1,$name1)->where($value2,'>=',$name2)->where('soft_deletion',2)->orderBy($orderBy,'DESC')->get();
        return $list;
    }

    //数据库增操作
    public function mysql_insert($table,$input)
    {
        return DB::table($table)->insertGetId($input);//新增
    }

    //数据库编辑操作
    public function mysql_edit($table,$ic,$input)
    {
        return DB::table($table)->where('ic',$ic)->update($input);//编辑
    }

    //数据库编辑操作
    public function mysql_where_edit($table,$where,$ic,$input)
    {
        return DB::table($table)->where($where,$ic)->update($input);//编辑
    }

    //数据库编辑操作
    public function mysql_two_edit($table,$where,$ic,$where1,$ic1,$input)
    {
        return DB::table($table)->where($where,$ic)->where($where1,$ic1)->update($input);//编辑
    }

    //数据库编辑操作
    public function mysql_three_edit($table,$where,$ic,$where1,$ic1,$where2,$ic2,$input)
    {
        return DB::table($table)->where($where,$ic)->where($where1,$ic1)->where($where2,$ic2)->update($input);//编辑
    }

    //数据库编辑操作(查找编号)
    public function mysql_num_edit($table,$xx_number,$num,$type,$input)
    {
        return DB::table($table)->where($xx_number,$num)->where('type',$type)->update($input);//编辑
    }

    //数据库删除操作(查找编号)
    public function mysql_num_del($table,$xx_number,$num,$type)
    {
        return DB::table($table)->where($xx_number,$num)->where('type',$type)->update(['soft_deletion'=>1]);//删除
    }

    //数据库删除操作
    public function mysql_del($table,$ic)
    {
        return DB::table($table)->where('ic',$ic)->update(['soft_deletion'=>1]);//删除
    }

    //数据库条件删除操作
    public function mysql_where_del($table,$value,$ic)
    {
        return DB::table($table)->where($value,$ic)->update(['soft_deletion'=>1]);//删除
    }

    //数据库删除操作
    public function mysql_del_true($table,$value,$ic)
    {
        return DB::table($table)->where($value,$ic)->delete();//删除
    }
    //数据库删除操作
    public function mysql_two_del_true($table,$value,$ic,$value1,$ic1)
    {
        return DB::table($table)->where($value,$ic)->where($value1,$ic1)->delete();//删除
    }

    //数据库自增操作
    public function mysql_where_increment($table,$where,$ic,$name,$value)
    {
        return DB::table($table)->where($where,$ic)->increment($name, $value);
    }

    //数据库自减操作
    public function mysql_where_decrement($table,$where,$ic,$name,$value)
    {
        return DB::table($table)->where($where,$ic)->decrement($name, $value);
    }

    //小程序模糊搜索
    public function Fuzzy_Search($table,$where,$value,$where1,$value1)
    {
        return DB::table($table)->where($where,'like',"%" .$value."%")
            ->where($where1,$value1)
            ->orderBy('created_at','DESC')
            ->get()->toArray();
    }

    //小程序模糊搜索两条件
    public function Fuzzy_Search_two($table,$where,$value,$where2,$value2,$orderBy)
    {
        return DB::table($table)->where($where,$value)
            ->where($where2,'like',"%" .$value2."%")
            ->orderBy($orderBy,'DESC')
            ->get()->toArray();
    }

    public function createdAllInSql($data,$b_id,$g_ic){
        $sql = '';
        $prefix = 'enterprises_good_batch';
        $date =$this->datas(1,'');
        $sql = "INSERT INTO `".$prefix."` (`enterprises_id`,`enterprises_goods_id`,`number`, `term_of_validity`, `date_of_manufacture`, `closing_date`, `created_at`, `time`) VALUES";
        foreach ($data as $item){
            $batch = $item['batch'];
            $deadline = $item['deadline'];
            $manufacture = $item['manufacture'];
            $validity = $item['validity'];
            $created_at = $date;
            $time = time()*1000;
            $sql .= "('$b_id','$g_ic','$batch', '$deadline', '$manufacture', '$validity', '$created_at', '$time'),";
        }
        $sql = substr($sql,0,strlen($sql)-1);
        return $sql;
    }

    public function createdAllInSql_code($code,$b_id,$g_ic,$b_g_b){
        $sql = '';
        $prefix = 'enterprises_good_batch_code';
        $date =$this->datas(1,'');
        $sql = "INSERT INTO `".$prefix."` (`enterprises_id`,`enterprises_goods_id`,`enterprises_good_batch_id`, `code`, `created_at`, `time`) VALUES";
        foreach ($code as $item){
            $batch = $item['code'];
            $created_at = $date;
            $time = time()*1000;
            $sql .= "('$b_id','$g_ic','$b_g_b', '$batch', '$created_at', '$time'),";
        }
        $sql = substr($sql,0,strlen($sql)-1);
        return $sql;
    }

    public function handle_array($handle)
    {
        return $handle->orderBy('created_at','DESC')->get()->toArray();
    }

    public function handle_array_two($handle,$where,$judge,$value,$where1,$judge1,$value1)
    {
        return $handle->where($where,$judge,$value)->where($where1,$judge1,$value1)->orderBy('created_at','DESC')->get()->toArray();
    }

    public function handle_array_three($handle,$where,$judge,$value,$where1,$judge1,$value1,$where2,$judge2,$value2)
    {
        return $handle->where($where,$judge,$value)->where($where1,$judge1,$value1)->where($where2,$judge2,$value2)->orderBy('created_at','DESC')->get()->toArray();
    }

    public function handle_array_four($handle,$where,$judge,$value,$where1,$judge1,$value1,$where2,$judge2,$value2,$where3,$judge3,$value3)
    {
        return $handle->where($where,$judge,$value)->where($where1,$judge1,$value1)->where($where2,$judge2,$value2)->where($where3,$judge3,$value3)->orderBy('created_at','DESC')->get()->toArray();
    }

    public function handle_array_one($handle,$where,$value,$orderBy)
    {
        return $handle->where($where,$value)->orderBy($orderBy,'DESC')->get()->toArray();
    }

    /*
        * 发起POST网络提交
        * @params string $url : 网络地址
        * @params json $data ： 发送的json格式数据
        */
    public function https_post($url,$data,$timeout=30)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /*
   * 发起GET网络提交
   * @params string $url : 网络地址
   */
    public function https_get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE) ;
        curl_setopt($curl, CURLOPT_TIMEOUT,60);
        if (curl_errno($curl)) {
            return 'Errno'.curl_error($curl);
        }
        else{$result=curl_exec($curl);}
        curl_close($curl);
        return $result;
    }

    function curl_datas($url,$data=[],$timeout=30)
    {
        $ch = curl_init();
        //取数据的地址
        curl_setopt($ch, CURLOPT_URL, $url);
        //传输为post
        curl_setopt($ch, CURLOPT_POST, true);
        //传输数据（这里data是二维数组，一定要加http_build_query，不然会报错）
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        //隐藏返回结果
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //限制时间
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        //https支持
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        //执行
        $handles = curl_exec($ch);
        //断开
        curl_close($ch);
        return $handles;
    }

    public function Log($authorizer_appid,$errcode,$describe,$details)
    {
        $date = $this->datas(1, '');
        $array['authorizer_appid'] = $authorizer_appid;
        $array['errcode'] = $errcode;
        $array['describe'] = $describe;
        $array['details'] = $details;
        $array['created_at'] = $date;
        $this->mysql_insert('log', $array);
    }
}